/**
 * Copyright (c) 2015-2016, Chill Zhuang 庄骞 (smallchill@163.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hd.common.utils;

import com.google.common.collect.Lists;
import com.hd.common.constant.BizConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;

@Slf4j
public final class DateUtil {

    public static String DATE_FORMAT_YYYYMMDD = "yyyyMMdd";

    /**
     * 获取YYYY格式
     *
     * @return
     */
    public static String getYear() {
        return formatDate(new Date(), "yyyy");
    }

    /**
     * 获取YYYY格式
     *
     * @return
     */
    public static String getYear(Date date) {
        return formatDate(date, "yyyy");
    }

    /**
     * 获取YYYY-MM-DD格式
     *
     * @return
     */
    public static String getDay() {
        return formatDate(new Date(), "yyyy-MM-dd");
    }

    /**
     * 获取YYYY-MM-DD格式
     *
     * @return
     */
    public static String getDay(Date date) {
        return formatDate(date, "yyyy-MM-dd");
    }


    /**
     * 自定义格式化的时间
     *
     * @return
     */
    public static String getDay(Date date, String pattern) {
        return formatDate(date, pattern);
    }

    /**
     * 获取YYYYMMDD格式
     *
     * @return
     */
    public static String getDays() {
        return formatDate(new Date(), "yyyyMMdd");
    }

    /**
     * 获取YYYYMMDD格式
     *
     * @return
     */
    public static String getDays(Date date) {
        return formatDate(date, "yyyyMMdd");
    }

    /**
     * 获取YYYY-MM-DD HH:mm:ss格式
     *
     * @return
     */
    public static String getTime() {
        return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
    }


    /**
     * 获取YYYY-MM-DD HH:mm:ss.SSS格式
     *
     * @return
     */
    public static String getMsTime() {
        return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS");
    }

    /**
     * 获取YYYYMMDDHHmmss格式
     *
     * @return
     */
    public static String getAllTime() {
        return formatDate(new Date(), "yyyyMMddHHmmss");
    }

    public static String getAllMillisecondTime() {
        return formatDate(new Date(), "yyyyMMddHHmmssSSS");
    }


    /**
     * 获取YYYY-MM-DD HH:mm:ss格式
     *
     * @return
     */
    public static String getTime(Date date) {
        if (date != null) {
            return formatDate(date, "yyyy-MM-dd HH:mm:ss");
        } else {
            return "";
        }
    }


    /**
     * 获取YYYY-MM-DD HH:mm格式
     *
     * @return
     */
    public static String getStandardTime(Date date) {
        if (date != null) {
            return formatDate(date, "yyyy-MM-dd HH:mm");
        } else {
            return "";
        }
    }


    public static String getUSDate(Date date, String pattern) {
        if (date != null) {
            return DateFormatUtils.format(date, pattern, Locale.US);
        } else {
            return "";
        }
    }


    public static String formatDate(Date date, String pattern) {
        if (date == null) {
            return "";
        }
        String formatDate = null;
        if (StringUtils.isNotBlank(pattern)) {
            formatDate = DateFormatUtils.format(date, pattern);
        } else {
            formatDate = DateFormatUtils.format(date, "yyyy-MM-dd");
        }
        return formatDate;
    }

    /**
     * @param s
     * @param e
     * @return boolean
     * @throws
     * @Title: compareDate
     * @Description:(日期比较，如果s>=e 返回true 否则返回false)
     * @author luguosui
     */
    public static boolean compareDate(String s, String e) {
        if (parseDate(s) == null || parseDate(e) == null) {
            return false;
        }
        return parseDate(s).getTime() >= parseDate(e).getTime();
    }

    public static boolean compareDate(Date s, Date e) {
        if (s == null || e == null) {
            return false;
        }
        return s.getTime() >= e.getTime();
    }


    /**
     * 格式化日期
     *
     * @return
     */
    public static Date parseDate(String date) {
        return parse(date, "yyyy-MM-dd");
    }

    /**
     * 格式化日期
     *
     * @return
     */
    public static Date parseTime(String date) {
        return parse(date, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 格式化日期
     *
     * @return
     */
    public static Date parse(String date, String pattern) {
        try {
            if (StringUtils.isNotBlank(date)) {
                return DateUtils.parseDate(date, pattern);
            } else {
                return null;
            }
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 格式化日期
     *
     * @return
     */
    public static String format(Date date, String pattern) {
        if (date != null) {
            return DateFormatUtils.format(date, pattern);
        } else {
            return null;
        }
    }

    /**
     * 把日期转换为Timestamp
     *
     * @param date
     * @return
     */
    public static Timestamp format(Date date) {
        return new Timestamp(date.getTime());
    }

    /**
     * 校验日期是否合法
     *
     * @return
     */
    public static boolean isValidDate(String s) {
        return parse(s, "yyyy-MM-dd HH:mm:ss") != null;
    }

    /**
     * 校验日期是否合法
     *
     * @return
     */
    public static boolean isValidDate(String s, String pattern) {
        return parse(s, pattern) != null;
    }

    public static int getDiffYear(String startTime, String endTime) {
        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        try {
            int years = (int) (((fmt.parse(endTime).getTime() - fmt.parse(
                    startTime).getTime()) / (1000 * 60 * 60 * 24)) / 365);
            return years;
        } catch (Exception e) {
            // 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
            return 0;
        }
    }

    /**
     * <li>功能描述：时间相减得到天数
     *
     * @param beginDateStr
     * @param endDateStr
     * @return long
     * @author Administrator
     */
    public static long getDaySub(String beginDateStr, String endDateStr) {
        long day = 0;
        SimpleDateFormat format = new SimpleDateFormat(
                "yyyy-MM-dd");
        Date beginDate = null;
        Date endDate = null;

        try {
            beginDate = format.parse(beginDateStr);
            endDate = format.parse(endDateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        day = (endDate.getTime() - beginDate.getTime()) / (24 * 60 * 60 * 1000);
        // System.out.println("相隔的天数="+day);

        return day;
    }

    /**
     * 得到n天之后的日期
     *
     * @param days
     * @return
     */
    public static String getAfterDayDate(String days) {
        int daysInt = Integer.parseInt(days);

        Calendar canlendar = Calendar.getInstance(); // java.util包
        canlendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动
        Date date = canlendar.getTime();

        SimpleDateFormat sdfd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = sdfd.format(date);

        return dateStr;
    }

    /**
     * 得到n天之后是周几
     *
     * @param days
     * @return
     */
    public static String getAfterDayWeek(String days) {
        int daysInt = Integer.parseInt(days);

        Calendar canlendar = Calendar.getInstance(); // java.util包
        canlendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动
        Date date = canlendar.getTime();

        SimpleDateFormat sdf = new SimpleDateFormat("E");
        String dateStr = sdf.format(date);

        return dateStr;
    }

    /**
     * 格式化Oracle Date
     * @param value
     * @return
     */
//	public static String buildDateValue(Object value){
//		if(Func.isOracle()){
//			return "to_date('"+ value +"','yyyy-mm-dd HH24:MI:SS')";
//		}else{
//			return Func.toStr(value);
//		}
//	}

    /**
     * 给时间加上几个小时
     *
     * @param date 当前时间 格式：yyyy-MM-dd HH:mm:ss
     * @param hour 需要加的时间
     * @return
     */
    public static Date addDateMinut(Date date, int hour) {
        if (date == null) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR, hour);// 24小时制
        date = cal.getTime();
        cal = null;
        return date;

    }


    /**
     * 给时间加上几十分钟
     *
     * @param date   当前时间 格式：yyyy-MM-dd HH:mm:ss
     * @param minute 需要加的分钟
     * @return
     */
    public static Date addDateMinuts(Date date, int minute) {
        if (date == null) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MINUTE, minute);// 24小时制
        date = cal.getTime();
        cal = null;
        return date;
    }

    /**
     * 计算两个时间相差多少秒
     *
     * @param endDate
     * @param startDate
     * @return
     */
    public static String getDiffSeconds(Date endDate, Date startDate) {
        if (endDate == null || startDate == null) return null;
        return ValueUtils.parseString((endDate.getTime() - startDate.getTime()) / 1000);
    }

    /**
     * @return java.lang.String
     * @author chentl
     * @description 计算两个时间相差多少分钟
     * @date 2021/1/6 11:05 上午
     * @params [endDate, startDate]
     */
    public static String getDiffMins(Date endDate, Date startDate) {
        if (endDate == null || startDate == null) return null;
        return ValueUtils.parseString((endDate.getTime() - startDate.getTime()) / (1000 * 60));
    }

    /**
     * @return java.lang.String
     * @author chentl
     * @description 根据秒数生成时分秒格式字符串
     * @date 2021/1/6 5:53 下午
     * @params [diffseconds]
     */
    public static String getDateDiffHmsTemplate(String diffseconds) {
        long diff = ValueUtils.parseLong(diffseconds);
        long day = diff / (24 * 60 * 60);
        long hour = (diff / (60 * 60) - day * 24);
        long min = ((diff / (60)) - day * 24 * 60 - hour * 60);
        long sec = (diff - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        return day + "天" + hour + "小时" + min + "分" + sec + "秒";

    }

    public static String getDatePoor(Date endDate, Date nowDate) {

        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return day + "天" + hour + "小时" + min + "分钟";
    }


    public static String getDatePoorExt(Date endDate, Date nowDate) {

        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        if (diff > 0) {
            // 计算差多少天
            long day = diff / nd;
            // 计算差多少小时
            long hour = diff % nd / nh;
            // 计算差多少分钟
            long min = diff % nd % nh / nm;
            // 计算差多少秒//输出结果
            // long sec = diff % nd % nh % nm / ns;
            return day + "天" + hour + "小时" + min + "分钟";
        } else {
            return "已超时";
        }
    }

    public static Date getNextDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, +1); //今天的时间加一天
        return calendar.getTime();
    }

    public static Date getBeforeDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, -1); //今天的时间减一天
        return calendar.getTime();
    }

    /**
     * 得到当前时间的五天前数据
     *
     * @return
     */
    public static Date getBeforeFiveDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, -5); //今天的时间减一天
        return calendar.getTime();
    }


    public static Date getNextDay(int days) {
        if (days > 0) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            calendar.add(Calendar.DAY_OF_MONTH, +days); //设置时间加days天
            return calendar.getTime();
        } else {
            return new Date();
        }
    }

    public static Date getNextDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, +1); //今天的时间加一天
        return calendar.getTime();
    }

    public static Date getNextDay(Date date, int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, days); //今天的时间加一天
        return calendar.getTime();
    }

    public static Integer getWeekOfDate(Date dt) {
        Integer[] weekDays = {7, 1, 2, 3, 4, 5, 6};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    public static Date getDate(String dateStr, String... formats) {
        Date date = null;
        for (String formatStr : formats) {
            try {
                SimpleDateFormat format = new SimpleDateFormat(formatStr);
                date = format.parse(dateStr);
                break;
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return date;
    }

    /**
     * compareStringDate:(日期比较，如果s>=e 返回true 否则返回false)
     *
     * @param s
     * @param e
     * @return
     * @since JDK 1.8
     */
    public static boolean compareStringDate(String s, String e) {
        int flag = s.compareTo(e);
        if (flag >= 0) {
            return true;
        } else {
            return false;
        }
    }


    public static boolean isEffectiveDate(Date date, Date startDate, Date endDate) {
        date = DateUtil.parseDate(DateUtil.format(date, "yyyy-MM-dd"));
        boolean flag = false;
        long time = date.getTime();
        long startTime = startDate.getTime();
        long endTime = endDate.getTime();
        if (time >= startTime && time <= endTime) {
            flag = true;
        }
        return flag;
    }

    /**
     * @param hourMinite 10:01
     * @param date
     * @return
     */
    public static Date joinDate(String hourMinite, Date date) {
        String stdDate = DateUtil.format(date, "yyyyMMdd");
        return DateUtil.parse(stdDate + " " + hourMinite, "yyyyMMdd HH:mm");
    }

    /**
     * @return String
     * @description 时间格式转换成英文时间格式(日 - 月)
     * @date 2020-07-20
     */
    public static String toUSDateStr(Date date) {
        try {
            SimpleDateFormat usFormat = new SimpleDateFormat("dd-MMM", Locale.US);//MMM dd hh:mm:ss Z yyyy
            return usFormat.format(date);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    /**
     * @return String
     * @description 时间格式转换成英文时间格式
     * @date 2020-07-20
     */
    public static String toUSDateStr(Date date, String usFormat) {
        try {
            SimpleDateFormat usDateFormat = new SimpleDateFormat(usFormat, Locale.US);//MMM dd hh:mm:ss Z yyyy
            return usDateFormat.format(date);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    /**
     * @param sourceFormat
     * @param targetFormat
     * @return String
     * @description 英文时间格式转换成数字时间格式
     * @date 2020-08-13
     */
    public static String parseUSDateStr(String timeStr, String sourceFormat, String targetFormat) {
        SimpleDateFormat sdf = new SimpleDateFormat(sourceFormat, Locale.US);//MMM dd hh:mm:ss Z yyyy
        try {
            Date usParseTime = sdf.parse(timeStr);
            return format(usParseTime, targetFormat);
        } catch (ParseException ex) {
            ex.printStackTrace();
            log.error("DateUtil parseUSDateStr exception:{}", ex.getMessage());
        }
        return null;
    }

    /**
     * @param sourceFormats
     * @param targetFormat
     * @return String
     * @description 英文时间格式转换成数字时间格式
     * @date 2020-08-13
     */
    public static String parseUSDateWithFormats(String timeStr, List<String> sourceFormats, String targetFormat) {
        for (String sourceFormat : sourceFormats) {
            SimpleDateFormat sdf = new SimpleDateFormat(sourceFormat, Locale.US);
            try {
                Date usParseTime = sdf.parse(timeStr);
                return format(usParseTime, targetFormat);
            } catch (ParseException ex) {
                log.error("DateUtil parseUSDateWithFormats exception:{}", ex.getMessage());
            }
        }
        return null;
    }


    /**
     * @return java.lang.String
     * @author chentl
     * @description 转换时间
     * @date 2020/12/31 12:11 下午
     * @params [milliseconds]
     */
    public static String transTime(long second) {
        final long day = TimeUnit.SECONDS.toDays(second);
        final long hours = TimeUnit.SECONDS.toHours(second) - TimeUnit.DAYS.toHours(TimeUnit.SECONDS.toDays(second));
        final long minutes = TimeUnit.SECONDS.toMinutes(second) - TimeUnit.HOURS.toMinutes(TimeUnit.SECONDS.toHours(second));
        final long seconds = TimeUnit.MILLISECONDS.toSeconds(second) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(second));

        return String.format("%d 天 %d 小时 %d 分种 %d 秒", day, hours, minutes, seconds);

    }

    /**
     * @return java.lang.String
     * @author chentl
     * @description 根据当前日期获取前一天日期的字符串
     * @date 2021/2/4 5:09 下午
     * @params [dateStr]
     */
    public static String getLastDayStr(String dateStr, int amount) {
        if (StringUtils.isBlank(dateStr)) return dateStr;
        Date current = parseDate(dateStr);
        Calendar c = Calendar.getInstance();
        c.setTime(current);
        c.add(Calendar.DAY_OF_MONTH, amount);
        Date last = c.getTime();
        return formatDate(last, "yyyy-MM-dd");
    }

    /**
     * @return java.lang.String
     * @author chentl
     * @description 根据当前日期获取上 amount 周的今天日期的字符串
     * @date 2021/2/5 2:38 下午
     * @params [dateStr, amount]
     */
    public static String getLastWeekStr(String dateStr, int amount) {
        if (StringUtils.isBlank(dateStr)) return dateStr;
        Date current = parseDate(dateStr);
        Calendar c = Calendar.getInstance();
        c.setTime(current);
        c.add(Calendar.WEEK_OF_YEAR, amount);
        Date last = c.getTime();
        return formatDate(last, "yyyy-MM-dd");
    }


    /**
     * @return java.lang.String
     * @author chentl
     * @description 根据当前月获取前 amount 月的字符串
     * @date 2021/2/5 10:43 上午
     * @params [dateStr]
     */
    public static String getLastMonthStr(String dateStr, int amount) {
        if (StringUtils.isBlank(dateStr)) return dateStr;
        Date current = parse(dateStr, "yyyy-MM");
        Calendar c = Calendar.getInstance();
        c.setTime(current);
        c.add(Calendar.MONTH, amount);
        Date last = c.getTime();
        return formatDate(last, "yyyy-MM");
    }

    /**
     * @return java.lang.String
     * @author chentl
     * @description 根据当前年获取前 amount 年的字符串
     * @date 2021/2/5 10:45 上午
     * @params [dateStr, amount]
     */
    public static String getLastYearStr(String dateStr, int amount) {
        if (StringUtils.isBlank(dateStr)) return dateStr;
        Date current = parse(dateStr, "yyyy");
        Calendar c = Calendar.getInstance();
        c.setTime(current);
        c.add(Calendar.YEAR, amount);
        Date last = c.getTime();
        return formatDate(last, "yyyy");
    }


    /**
     * @return com.hd.common.utils.Pair<java.lang.String, java.lang.String>
     * @author chentl
     * @description 获取X天开始 结束
     * @date 2021/2/5 10:38 上午
     * @params [dateStr, pre]
     */
    public static Pair<String, String> getDayScope(String dateStr, boolean pre) {
        if (StringUtils.isBlank(dateStr)) return Pair.makePair("", "");
        if (pre) dateStr = getLastDayStr(dateStr, -1);

        String firstDay, lastDay;
        firstDay = DateUtil.fillDate(dateStr, "start");
        lastDay = DateUtil.fillDate(dateStr, "end");
        return Pair.makePair(firstDay, lastDay);
    }

    /**
     * @return com.hd.common.utils.Pair<java.lang.String, java.lang.String>
     * @author chentl
     * @description 获取X周开始 结束
     * @date 2021/2/5 2:26 下午
     * @params [dateStr, pre]
     */
    public static Pair<String, String> getWeekScope(String dateStr, boolean pre) {
        if (StringUtils.isBlank(dateStr)) return Pair.makePair("", "");
        if (pre) dateStr = getLastWeekStr(dateStr, -1);

        String firstDay, lastDay;

        Calendar c = Calendar.getInstance();
        c.setTime(Objects.requireNonNull(parse(dateStr, "yyyy-MM-dd")));

        int d;
        if (c.get(Calendar.DAY_OF_WEEK) == 1) {
            d = -6;
        } else {
            d = 2 - c.get(Calendar.DAY_OF_WEEK);
        }
        c.add(Calendar.DAY_OF_WEEK, d);
        // 所在周开始日期
        firstDay = DateUtil.fillDate(formatDate(c.getTime(), "yyyy-MM-dd"), "start");
        c.add(Calendar.DAY_OF_WEEK, 6);
        // 所在周结束日期
        lastDay = DateUtil.fillDate(formatDate(c.getTime(), "yyyy-MM-dd"), "end");
        return Pair.makePair(firstDay, lastDay);
    }

    /**
     * @return com.hd.common.utils.Pair<java.lang.String, java.lang.String>
     * @author chentl
     * @description 获取X月第一天和最后一天
     * @date 2021/2/5 10:02 上午
     * @params [dateStr, pre]
     */
    public static Pair<String, String> getMonthScope(String dateStr, boolean pre) {
        if (StringUtils.isBlank(dateStr)) return Pair.makePair("", "");
        if (pre) dateStr = getLastMonthStr(dateStr, -1);

        Date currentDate = parse(dateStr, "yyyy-MM");

        String firstDay, lastDay;
        Calendar c = Calendar.getInstance();
        c.setTime(currentDate);
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 1);
        firstDay = DateUtil.fillDate(formatDate(c.getTime(), "yyyy-MM-dd"), "start");
        c = Calendar.getInstance();
        c.setTime(currentDate);
        c.add(Calendar.MONTH, 1);
        c.set(Calendar.DAY_OF_MONTH, 0);
        lastDay = DateUtil.fillDate(formatDate(c.getTime(), "yyyy-MM-dd"), "end");

        return Pair.makePair(firstDay, lastDay);
    }

    /**
     * @return com.hd.common.utils.Pair<java.lang.String, java.lang.String>
     * @author chentl
     * @description 获取X年第一天和最后一天
     * @date 2021/2/5 10:09 上午
     * @params [dateStr, pre]
     */
    public static Pair<String, String> getYearScope(String dateStr, boolean pre) {
        if (StringUtils.isBlank(dateStr)) return Pair.makePair("", "");
        if (pre) dateStr = getLastYearStr(dateStr, -1);

        Date currentYear = parse(dateStr, "yyyy");

        String firstDay, lastDay;
        int year = Integer.parseInt(dateStr);
        Calendar c = Calendar.getInstance();
        c.setTime(currentYear);

        c.clear();
        c.set(Calendar.YEAR, year);
        firstDay = DateUtil.fillDate(formatDate(c.getTime(), "yyyy-MM-dd"), "start");

        c.clear();
        c.set(Calendar.YEAR, year);
        c.roll(Calendar.DAY_OF_YEAR, -1);
        lastDay = DateUtil.fillDate(formatDate(c.getTime(), "yyyy-MM-dd"), "end");

        return Pair.makePair(firstDay, lastDay);
    }

    /**
     * @return com.hd.common.utils.Pair<java.lang.String, java.lang.String>
     * @author chentl
     * @description 获取 周、月、年 前6周期时间起始字符串
     * @date 2021/2/5 5:05 下午
     * @params [dateStr, timeFlag]
     */
    public static Pair<String, String> getRecent6Range(String dateStr, String timeFlag) {
        if (StringUtils.isBlank(dateStr)) return Pair.makePair("", "");

        String firstDay = "", lastDay = "";
        if (BizConstant.Common.DAY.key().equalsIgnoreCase(timeFlag)) {
            //获取周趋势 开始结束  当前时间所在周最后一天、当前时间所在周的前 6周的第一天
            firstDay = getDayScope(getLastDayStr(dateStr, -6), false).first;
            lastDay = getDayScope(dateStr, false).second;
        } else if (BizConstant.Common.WEEK.key().equalsIgnoreCase(timeFlag)) {
            //获取周趋势 开始结束  当前时间所在周最后一天、当前时间所在周的前 6周的第一天
            firstDay = getWeekScope(getLastWeekStr(dateStr, -6), false).first;
            lastDay = getWeekScope(dateStr, false).second;
        } else if (BizConstant.Common.MONTH.key().equalsIgnoreCase(timeFlag)) {
            //获取月趋势 开始结束 当前时间所在月最后一天、当前时间所在月的前 6月的第一天
            firstDay = getMonthScope(getLastMonthStr(dateStr, -6), false).first;
            lastDay = getMonthScope(dateStr, false).second;
        } else if (BizConstant.Common.YEAR.key().equalsIgnoreCase(timeFlag)) {
            //获取年趋势 开始结束 当前时间所在年最后一天、当前时间所在年的前 6年的第一天
            firstDay = getYearScope(getLastYearStr(dateStr, -6), false).first;
            lastDay = getYearScope(dateStr, false).second;
        }

        return Pair.makePair(firstDay, lastDay);

    }

    /**
     * @return java.util.List<java.lang.String>
     * @author chentl
     * @description 获取趋势日期字符串集合
     * @date 2021/2/18 11:54 上午
     * @params [dateStr, timeFlag]
     */
    public static List<String> getRecent6DateScope(String dateStr, String timeFlag) {
        List<String> dateList = Lists.newArrayList();
        if (BizConstant.Common.DAY.key().equalsIgnoreCase(timeFlag)) {
            dateList.add(dateStr);
            for (int i = 1; i < 6; i++) {
                dateList.add(getLastDayStr(dateStr, -i));
            }
        } else if (BizConstant.Common.WEEK.key().equalsIgnoreCase(timeFlag)) {
            dateList.add(getWeekOfYear(dateStr));
            for (int i = 1; i < 6; i++) {
                dateList.add(getWeekOfYear(getLastWeekStr(dateStr, -i)));
            }
        } else if (BizConstant.Common.MONTH.key().equalsIgnoreCase(timeFlag)) {
            dateList.add(getMonthOfYear(dateStr));
            for (int i = 1; i < 6; i++) {
                dateList.add(getMonthOfYear(getLastMonthStr(dateStr, -i)));
            }
        } else if (BizConstant.Common.YEAR.key().equalsIgnoreCase(timeFlag)) {
            dateList.add(getYear(dateStr));
            for (int i = 1; i < 6; i++) {
                dateList.add(getYear(getLastYearStr(dateStr, -i)));
            }
        }
        return dateList;
    }

    /**
     * @return java.lang.String
     * @author chentl
     * @description 获取一年的第几周
     * @date 2021/2/18 1:55 下午
     * @params [dateStr]
     */
    public static String getWeekOfYear(String dateStr) {
        if (StringUtils.isBlank(dateStr)) return "";

        Date date = parseDate(dateStr);
        Calendar cal = Calendar.getInstance();
        cal.setFirstDayOfWeek(Calendar.MONDAY);//设置周一为一周的第一天
        cal.setTime(date);
        int week = cal.get(Calendar.WEEK_OF_YEAR);
        int year = cal.get(Calendar.YEAR);
        return year + "_" + week;
    }

    /**
     * @return java.lang.String
     * @author chentl
     * @description 获取一年的第几月
     * @date 2021/2/18 2:10 下午
     * @params [dateStr]
     */
    public static String getMonthOfYear(String dateStr) {
        if (StringUtils.isBlank(dateStr)) return "";

        Date date = parse(dateStr, "yyyy-MM");
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int month = cal.get(Calendar.MONTH) + 1;
        int year = cal.get(Calendar.YEAR);
        return year + "_" + month;
    }

    /**
     * @return java.lang.String
     * @author chentl
     * @description 获取当前年份
     * @date 2021/2/18 2:12 下午
     * @params [dateStr]
     */
    public static String getYear(String dateStr) {
        if (StringUtils.isBlank(dateStr)) return "";

        Date date = parse(dateStr, "yyyy");
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int year = cal.get(Calendar.YEAR);
        return String.valueOf(year);
    }


    /**
     * @return java.lang.String
     * @author chentl
     * @description 填充开始时间、结束时间
     * @date 2021/1/19 1:10 下午
     * @params [dateStr, fillType]
     */
    public static String fillDate(String dateStr, String fillType) {
        if (StringUtils.isBlank(dateStr)) return dateStr;
        if ("start".equals(fillType)) return dateStr + " 00:00:00";
        if ("end".equals(fillType)) return dateStr + " 23:59:59";
        return dateStr;
    }

    /**
     * @return
     * @author chentl
     * @description 获取今天零点零分零秒
     * @date 2021/2/5 11:59 上午
     * @params []
     */
    public static Date getToDayOrigin() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    public static void main(String[] args) throws ParseException {
        getRecent6DateScope("2021-02-05", "w");

        getYearScope("2000", true);
        getMonthScope("2021-12", false);
        getLastWeekStr("2021-02-05", -6);

        Date end = parse("2021-01-28 13:36:11", "yyyy-MM-dd HH:mm:ss");
        Date start = parse("2021-01-28 13:23:11", "yyyy-MM-dd HH:mm:ss");
        System.out.println(getDiffMins(end, start));

        System.out.println(getDiffSeconds(end, start));

        System.out.println(getDateDiffHmsTemplate(getDiffSeconds(end, start)));

    }


}
